package com.swufe.forfree.ui.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ASUS on 2018/6/14.
 */

public class DBManager {

    private DBHelper dbHelper;
    private String TBNAME;

    public DBManager(Context context) {
        dbHelper = new DBHelper(context);
        TBNAME = DBHelper.TB_NAME;
    }

    public void add(RadiumBean item) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("curUrl", item.getCurUrl());
        values.put("curNames", item.getCurNames());
        values.put("curImgs", item.getCurImgs());
        db.insert(TBNAME, null, values);
        db.close();
    }

    public void addAll(List<RadiumBean> list) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        for (RadiumBean item : list) {
            ContentValues values = new ContentValues();
            values.put("curUrl", item.getCurUrl());
            values.put("curNames", item.getCurNames());
            values.put("curImgs", item.getCurImgs());
            db.insert(TBNAME, null, values);
        }
        db.close();
    }

    public void deleteAll(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }

    public void delete(int id){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME, "ID=?", new String[]{String.valueOf(id)});
        db.close();
    }

    public void update(RadiumBean item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("curUrl", item.getCurUrl());
        values.put("curNames", item.getCurNames());
        values.put("curImgs", item.getCurImgs());
        db.update(TBNAME, values, "ID=?", new String[]{String.valueOf(item.getId())});
        db.close();
    }

    public List<RadiumBean> listAll(){
        List<RadiumBean> beanList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null,
                null, null, null, null);
        if(cursor!=null) {
            beanList = new ArrayList<RadiumBean>();
            while(cursor.moveToNext()){
                RadiumBean item = new RadiumBean();
                item.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                item.setCurUrl(cursor.getString(cursor.getColumnIndex("CURURL")));
                item.setCurNames(cursor.getString(cursor.getColumnIndex("CURNAMES")));
                item.setCurImgs(cursor.getString(cursor.getColumnIndex("CURIMGS")));
                beanList.add(item);
            }
            cursor.close();
        }
        db.close();
        return beanList;
    }

    public RadiumBean findById(int id) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, "ID=?",
                new String[]{String.valueOf(id)}, null, null, null);
        RadiumBean beanItem = null;
        if (cursor != null && cursor.moveToFirst()) {
            beanItem = new RadiumBean();
            beanItem.setId(cursor.getInt(cursor.getColumnIndex("ID")));
            beanItem.setCurUrl(cursor.getString(cursor.getColumnIndex("CURURL")));
            beanItem.setCurNames(cursor.getString(cursor.getColumnIndex("CURNAMES")));
            beanItem.setCurImgs(cursor.getString(cursor.getColumnIndex("CURIMGS")));
            cursor.close();
        }
        db.close();
        return beanItem;
    }
}
